package com.example.home.government.acivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.government.adapter.GovernmentAdapter;
import com.example.home.government.adapter.MeAppealAdapter;
import com.example.home.government.adapter.PersonAppealAdapter;
import com.example.home.government.bean.BannerGover;
import com.example.home.government.bean.MeAppealBean;
import com.example.home.government.bean.PersonAppealBean;
import com.example.home.tool.OKHttpTool;

import java.util.Timer;
import java.util.TimerTask;

public class GovernmentMainActivity extends AppCompatActivity {

    private RecyclerView rcv_serve;

    private Timer timer;
    private int currentPage = 0;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 3000;
    private GovernmentAdapter adapter;
    private ViewPager vp_banner;
    private RecyclerView rcv_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_government_main);
        initView();
        //轮播图
        getBanner();
        startAutoScroll();

        //市民诉求分类
        getPersonAppeal();

        //我的诉求
        getMeAppeal();
    }

    protected void onResume() {
        super.onResume();
        // 重新加载数据的逻辑
        //我的诉求
        getMeAppeal();
    }

        private void getMeAppeal() {
        OKHttpTool.doNetRequest("/prod-api/api/gov-service-hotline/appeal/my-list",
                OKHttpTool.GET,
                MeAppealBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        MeAppealBean meAppealBean = (MeAppealBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindMeAppeal(meAppealBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindMeAppeal(MeAppealBean meAppealBean) {
        RecyclerView recyclerView = findViewById(R.id.rcv_list);
        MeAppealAdapter meAppealAdapter = new MeAppealAdapter(GovernmentMainActivity.this,meAppealBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getBaseContext());
        recyclerView.setAdapter(meAppealAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    private void getPersonAppeal() {
        OKHttpTool.doNetRequest("prod-api/api/gov-service-hotline/appeal-category/list",
                OKHttpTool.GET,
                PersonAppealBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        PersonAppealBean personAppealBean = (PersonAppealBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindAppeal(personAppealBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindAppeal(PersonAppealBean personAppealBean) {
        RecyclerView recyclerView = findViewById(R.id.rcv_serve);
        PersonAppealAdapter adapter = new PersonAppealAdapter(GovernmentMainActivity.this,personAppealBean.getRows());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(GovernmentMainActivity.this, 2);
        gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    private void getBanner() {
        OKHttpTool.doNetRequest("prod-api/api/gov-service-hotline/ad-banner/list",
                OKHttpTool.GET,
                BannerGover.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        BannerGover gover = (BannerGover) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindSlideshow(gover);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindSlideshow(BannerGover gover) {
        ViewPager viewPager = findViewById(R.id.vp_banner);
        adapter = new GovernmentAdapter(this, gover.getData());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == adapter.getCount()) {
                    currentPage = 0;
                }
                vp_banner.setCurrentItem(currentPage++, true);
                ;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

    private void initView() {
        ImageView back = findViewById(R.id.back);
        vp_banner = findViewById(R.id.vp_banner);
        rcv_serve = findViewById(R.id.rcv_serve);
        rcv_list = findViewById(R.id.rcv_list);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GovernmentMainActivity.this.finish();
            }
        });
    }
}